﻿package view
{
	import controller.GameController;
	
	import fl.transitions.Tween;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import model.Question;
	
	public class Explanation extends MovieClip
	{
		
		private var question:Question;
		private var gameControl:GameController;
		
		private var handledMistakes:int = 0;
		private var mistakes:Array;
		private var numberOfMistakes:int;
		private var wrongs:int = 0;
		private var rights:int = 0;
		private var passed:Boolean;
		
		private var timer:Timer = new Timer(1000, 1);
		
		private var noMistakesSound:Sound = new Sound;
		private var taskFinished:Sound = new Sound;
		private var taskNotFinished:Sound = new Sound;
		private var shouldHaveCatched:Sound = new Sound;
		private var shouldHaveNotCatched:Sound = new Sound;
		private var earnedStarsSound:Sound = new Sound;
		private var nextTask1:Sound = new Sound;
		private var nextTask2:Sound = new Sound;
				
		private var soundChannel:SoundChannel = new SoundChannel();
		
		private var cart:Cart;
		
		
		/**
		 * läd die Basis-Sounds
		 */
		public function Explanation()
		{
			noMistakesSound.load(new URLRequest(Config.OtherSoundPath + "alles_richtig.mp3"));
			taskFinished.load(new URLRequest(Config.OtherSoundPath + "aufgabe_geschafft.mp3"));
			taskNotFinished.load(new URLRequest(Config.OtherSoundPath + "aufgabe_nicht_geschafft.mp3"));
			shouldHaveCatched.load(new URLRequest(Config.OtherSoundPath + "fangen_sollen.mp3"));
			shouldHaveNotCatched.load(new URLRequest(Config.OtherSoundPath + "nicht_fangen_sollen.mp3"));
			nextTask1.load(new URLRequest(Config.OtherSoundPath + "aufgabe1.mp3"));
			nextTask2.load(new URLRequest(Config.OtherSoundPath + "aufgabe2.mp3"));
			
		}
		
		public function startExplanation(question:Question, gameControl:GameController, wrongMistakes:Array, numberOfMistakes:int, noMistakes:Boolean, cart:Cart):void
		{
			this.cart = cart;
			this.mistakes = wrongMistakes;
			this.numberOfMistakes = numberOfMistakes;
			this.question = question;
			this.gameControl = gameControl;
			this.passed = noMistakes;
			for(var i:int = 0; i < mistakes.length; i++){
				this.gameControl.stopGlow(mistakes[i]);
				this.mistakes[i].alpha = 0;
			}
			
			calcEarnedStars();
			setRightsAndWrongs();
			
			//Es wurden Fehler gemacht
			if(mistakes.length > 0){
				//Spielende durch 5 gefangene Produkte
				if(noMistakes){
					this.soundChannel = this.taskFinished.play();
					this.soundChannel.addEventListener(Event.SOUND_COMPLETE, notCatchedProduct);
					//Spielende durch 3 Fehler
				}else{
					this.soundChannel = this.taskNotFinished.play();
					this.soundChannel.addEventListener(Event.SOUND_COMPLETE, notCatchedProduct);					
				}				
			}else{
				this.soundChannel = this.noMistakesSound.play();
				this.soundChannel.addEventListener(Event.SOUND_COMPLETE, function(){
					starsFly();
				});
			}
			this.cart.startFaceMoving(this.soundChannel);
		}
		
		private function setRightsAndWrongs():void
		{
			for(var i:int = 0; i < mistakes.length; i++){
				if(!this.question.checkProduct(mistakes[i].getProduct())){
					this.wrongs++;
				}else{
					this.rights++;
				}
			}	
		}
		
		private function notCatchedProduct(e:Event):void
		{
			gameControl.showNote(450,200, true);
			this.soundChannel.removeEventListener(Event.SOUND_COMPLETE, notCatchedProduct);	
			if(wrongs == 0){
				trace("wrongs " + wrongs);
				this.soundChannel = this.shouldHaveCatched.play();
				this.soundChannel.addEventListener(Event.SOUND_COMPLETE, shouldCatched);
			}else{
				this.soundChannel = this.shouldHaveNotCatched.play();
				this.soundChannel.addEventListener(Event.SOUND_COMPLETE, shouldNotCatched);
			}
			this.cart.startFaceMoving(this.soundChannel);
		}
		
		private function shouldNotCatched(e:Event):void
		{		
			this.soundChannel.removeEventListener(Event.SOUND_COMPLETE, shouldNotCatched);
			this.timer.removeEventListener(TimerEvent.TIMER_COMPLETE, shouldNotCatched);
			if(handledMistakes < mistakes.length){
				if(!this.question.checkProduct(mistakes[handledMistakes].getProduct())){
					// falsches Objekt
					var productWrapper:ProductWrapper = mistakes[handledMistakes];
					mistakes[handledMistakes].alpha = 1;
					mistakes[handledMistakes].x = 550;
					mistakes[handledMistakes].y = 280;
					
					var tween:Tween = new Tween(mistakes[handledMistakes], "scaleX", null, 0, 1, 1, true);
					tween = new Tween(mistakes[handledMistakes], "scaleY", null, 0, 1, 1, true);
					this.soundChannel = this.mistakes[handledMistakes].getProduct().getNameSound().play();
					this.soundChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
				}else{
					handledMistakes++;
					shouldNotCatched(null);
				}
			}else{
				if(handledMistakes == mistakes.length){
					if(this.rights == 0){
						starsFly();
					}else{
						this.soundChannel = this.shouldHaveCatched.play();
						this.soundChannel.addEventListener(Event.SOUND_COMPLETE, shouldCatched);
					}					
					handledMistakes = 0;
				}
				
			}
			this.cart.startFaceMoving(this.soundChannel);
		}
		
		private function soundComplete(e:Event):void
		{
			this.soundChannel.removeEventListener(Event.SOUND_COMPLETE, soundComplete);
			this.soundChannel = this.mistakes[handledMistakes].getProduct().getExplanationSound().play();
			this.soundChannel.addEventListener(Event.SOUND_COMPLETE, function(){
				var tweenDown:Tween = new Tween(mistakes[handledMistakes], "scaleX", null, 1, 0.5, 1, true);
				tweenDown = new Tween(mistakes[handledMistakes], "scaleY", null, 1, 0.5, 1, true);
				tweenDown = new Tween(mistakes[handledMistakes], "y", null, mistakes[handledMistakes].y, 450, 1, true);
				tweenDown = new Tween(mistakes[handledMistakes], "x", null, mistakes[handledMistakes].x, 470 + (handledMistakes * 50), 1, true);
				
				timer.start();	
				timer.addEventListener(TimerEvent.TIMER_COMPLETE, shouldNotCatched);
				handledMistakes++;
			});
			this.cart.startFaceMoving(this.soundChannel);
		}
		
		
		private function soundCompleteCatched(e:Event):void
		{
			this.soundChannel.removeEventListener(Event.SOUND_COMPLETE, soundCompleteCatched);
			this.soundChannel = this.mistakes[handledMistakes].getProduct().getExplanationSound().play();
			this.soundChannel.addEventListener(Event.SOUND_COMPLETE, function(){
				var tweenDown:Tween = new Tween(mistakes[handledMistakes], "scaleX", null, 1, 0.5, 1, true);
				tweenDown = new Tween(mistakes[handledMistakes], "scaleY", null, 1, 0.5, 1, true);
				tweenDown = new Tween(mistakes[handledMistakes], "y", null, mistakes[handledMistakes].y, 400, 1, true);
				tweenDown = new Tween(mistakes[handledMistakes], "x", null, mistakes[handledMistakes].x, 600 - (handledMistakes * 50), 1, true);
				
				timer.addEventListener(TimerEvent.TIMER_COMPLETE, shouldCatched);
				timer.start();
				handledMistakes++;
				
			});
			this.cart.startFaceMoving(this.soundChannel);
		}
		
		private function shouldCatched(e:Event):void
		{
			this.timer.removeEventListener(TimerEvent.TIMER_COMPLETE, shouldCatched);
			if(handledMistakes < mistakes.length){
				if(this.question.checkProduct(mistakes[handledMistakes].getProduct())){					
					mistakes[handledMistakes].x = 550;
					mistakes[handledMistakes].y = 280;
					mistakes[handledMistakes].alpha = 1;
					var tween:Tween = new Tween(mistakes[handledMistakes], "scaleX", null, 0, 1, 1, true);
					tween = new Tween(mistakes[handledMistakes], "scaleY", null, 0, 1, 1, true);
					this.soundChannel = this.mistakes[handledMistakes].getProduct().getNameSound().play();
					this.soundChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleteCatched);
				}else{
					handledMistakes++;
					shouldCatched(null);
				}
			}else{
				starsFly();
			}
			this.cart.startFaceMoving(this.soundChannel);
		}
		
		private function calcEarnedStars():void
		{
			var earnedPoints:int = 4-numberOfMistakes;
			this.earnedStarsSound = new Sound;
			this.earnedStarsSound.load(new URLRequest(Config.OtherSoundPath + "stars" + earnedPoints + ".mp3"));
			this.earnedStarsSound.addEventListener(Event.COMPLETE, test);
		}
	
		private function test(e:Event):void{
			trace("Sound geladen");
		}
		
		private function starsFly():void
		{
			if(this.earnedStarsSound.bytesLoaded == this.earnedStarsSound.bytesTotal){
					this.soundChannel = this.earnedStarsSound.play();
					this.soundChannel.addEventListener(Event.SOUND_COMPLETE, goToGameControl);
					this.cart.startFaceMoving(this.soundChannel);
			}else{
				starsFly();
			}	
			
		}
		
		private function goToGameControl(e:Event):void
		{
			this.soundChannel.removeEventListener(Event.SOUND_COMPLETE, goToGameControl);
			this.gameControl.setPoints(4 - numberOfMistakes);
		}
		
		public function nextTask():void
		{
			for(var i:int = 0; i < mistakes.length; i++){
				mistakes[i].alpha = 0;
			}
			if(passed && gameControl.getCurrentLevel() != 5){				
				var sound:int = (Math.floor((Math.random()*2)))+1;
				this.soundChannel = this["nextTask"+sound].play();
				this.soundChannel.addEventListener(Event.SOUND_COMPLETE, startNextTask);
				this.cart.startFaceMoving(this.soundChannel);
			}else{
				//Spielende --> geh zum Score
				this.gameControl.callScore();
				
			}
			
			this.wrongs = 0;
			this.rights = 0;
			//this.cart.startFaceMoving(this.soundChannel);
			this.gameControl.hideNote();
		}
		
		private function startNextTask(e:Event):void
		{
			this.cart.stopFaceMoving(e);
			this.gameControl.cleanUp();
			this.handledMistakes = 0;
			this.mistakes = null;
			this.numberOfMistakes = 0;
			this.earnedStarsSound = null;
			this.earnedStarsSound = new Sound;
			this.soundChannel = new SoundChannel;
		}
	}
}